layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate;

    //日期
    laydate.render({
        elem: '#date'
    });
    laydate.render({
        elem: '#date1'
    });





});

$user=$('#user').val();
if($user===""){
    layer.msg("您还没有登陆哦！");
    window.location.href="login.html";

}
var uploadUrl = '../php/admin/upload.php';
//文件选择完毕时
function FileChangeFn(event) {
    $('.opst_txt').text('重新选择文件');
    $('.send_btn').show();
    var event = event || window.event,
        dom = '',
        ofile = $("#oFile").get(0).files[0],
        otype = ofile.type || '获取失败',
        osize = ofile.size / 1054000,
        ourl = window.URL.createObjectURL(ofile); //文件临时地址
    $('#file_type').text("选择上传文件类型：" + otype);
    $('#file_size').text("选择上传文件大小，共" + osize.toFixed(2) + "MB。");

    console.log("文件类型：" + otype); //文件类型
    console.log("文件大小：" + osize); //文件大小

    if ('video/mp4' == otype || 'video/avi' == otype || 'video/x-msvideo' == otype) {
        layer.msg("请上传压缩文件/或者图片");
    }
    if ('audio/mp3' == otype || 'audio/wav' == otype  || 'audio/x-m4a' == otype) {
        layer.msg("请上传压缩文件/或者图片");
    }
    if ('image/jpeg' == otype || 'image/png' == otype || 'image/gif' == otype) {
        dom = '<img id="photo" width="100px"  alt="我是image图片文件" src=' + ourl + ' title="" />';
    }
    if ('zip' == otype) {
        dom = '<img id="photo" width="100px"  alt="我是压缩文件" src=' + ourl + ' title="" />';
    }
    $('#file_box').html(dom);
};

//侦查附件上传情况 ,这个方法大概0.05-0.1秒执行一次
function OnProgRess(event) {
    var event = event || window.event;
    //console.log(event);  //事件对象
    console.log("已经上传：" + event.loaded); //已经上传大小情况(已上传大小，上传完毕后就 等于 附件总大小)
    //console.log(event.total);  //附件总大小(固定不变)
    var loaded = Math.floor(100 * (event.loaded / event.total)); //已经上传的百分比
    $("#speed").html(loaded + "%").css("width", loaded + "%");
};

//开始上传文件
function UploadFileFn() {


    var file=document.getElementById("oFile");
    fileV=file.value;
    if(fileV===""){
        layer.msg("您还没有选择任何文件哦")
    }
    else {

        $('.speed_box').show();
        var oFile = $("#oFile").get(0).files[0], //input file标签
            formData = new FormData(); //创建FormData对象
        xhr = $.ajaxSettings.xhr(); //创建并返回XMLHttpRequest对象的回调函数(jQuery中$.ajax中的方法)
        formData.append("myFile", oFile); //将上传name属性名(注意：一定要和 file元素中的name名相同)，和file元素追加到FormData对象中去

        $.ajax({
            type: "POST",
            url: uploadUrl, // 后端服务器上传地址
            data: formData, // formData数据
            cache: false, // 是否缓存
            async: true, // 是否异步执行
            processData: false, // 是否处理发送的数据  (必须false才会避开jQuery对 formdata 的默认处理)
            contentType: false, // 是否设置Content-Type请求头
            dataType: 'json',
            xhr: function () {
                if (OnProgRess && xhr.upload) {
                    xhr.upload.addEventListener("progress", OnProgRess, false);
                    return xhr;
                }
            },
            success: function (dat) {

                $("#speed").html("上传成功");
                var url=document.getElementById("file-url");
                url.value=dat.src;
            },
            error: function (dat) {
                $("#speed").html("上传失败");

                layer.msg('请正确配置后台服务！');
            }
        });
    }
}

function email() {
    var task_content =document.getElementById("task_content");
    if (task_content.value === '') {
        layer.msg ('你的任务还未填写');
        return false;


    }
    else {
        var user=document.getElementById("user"),
            task_a=document.getElementById("task_a"),
            date=document.getElementById("date"),
            task_a=document.getElementById("task_a"),
            UserA=document.getElementById("selectOne"),
            bei=document.getElementById("bei-zhu"),
            url=document.getElementById("file-url");
        $user=user.value;
        $bei=bei.value;
        $date=date.value;
        $userA=UserA.value;
        $task_a=task_a.value;
        $task_a=task_a.value;
        $url=url.value;
        $task_content = task_content.value;
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../php/phpmail/sendmail.php',
            data: {
                "task_content": $task_content,
                "user":$user,
                "task_a":$task_a,
                "date":$date,
                "taske_a":$task_a,
                "userA":$userA,
                "remarks":$bei,

            },
            dataType: 'Json',
            success: function (dat) {
                layer.msg(dat.message);
            },
            error: function (dat) {
                layer.msg("发送邮件失败，请检查您的网络");
            }


        })


    }
}

function release() {
    var task_content =document.getElementById("task_content");
    if (task_content.value === '') {
        layer.msg ('你的任务还未填写');
        return false;


    }
    else {
        var user=document.getElementById("user"),
            task_a=document.getElementById("task_a"),
            date=document.getElementById("date"),
            task_a=document.getElementById("task_a"),
            UserA=document.getElementById("selectOne"),
            bei=document.getElementById("bei-zhu"),
            url=document.getElementById("file-url");
        $user=user.value;
        $bei=bei.value;
        $date=date.value;
        $userA=UserA.value;
        $task_a=task_a.value;
        $task_a=task_a.value;
        $url=url.value;
        $task_content = task_content.value;
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../php/admin/add_task.php',
            data: {
                "task_content": $task_content,
                "user":$user,
                "task_a":$task_a,
                "date":$date,
                "taske_a":$task_a,
                "userA":$userA,
                "remarks":$bei,
                "url":$url,
            },
            dataType: 'Json',
            success: function (dat) {
                layer.msg(dat.message);


            },
            error: function (dat) {
                layer.msg("发布失败");
            }


        })


    }







}

$.ajax({
    type: 'post',
    cache: false,
    url: '../php/admin/select_notice.php',
    data: {
        "user":$user,
    },
    dataType: 'json',
    success: function (data) {
        $dat = data.data;
        $coun = data.count;
        $notice=$dat[0].content;
        var not=document.getElementById("notice");
        not.innerHTML=$notice;


    },
    error: function (data) {

    },
});

$.ajax({
    type: 'post',
    cache: false,
    url: '../php/user/select_user_class.php?class=<?php echo $group ?>',
    data: {
    },
    dataType: 'json',
    success: function (data) {
        $coun = data.count;
        $dat = data.data;
        var select=document.getElementById("selectOne");
        for ($i = 0; $i < $coun; $i++) {
            $product = $dat[$i].user;
            $email=$dat[$i].email;
            select.innerHTML += "<option value='"+$product+"' >"+$product+"</option>";
        }
        layui.form.render('select');

    },
    error: function (data) {
        layer.msg("查询出错");
    }
});


$.ajax({
    type: 'post',
    cache: false,
    url: '../../php/admin/select_task.php',
    data: {
        "user":$user,
    },
    dataType: 'json',
    success: function (data) {
        $coun = data.count;
        $dat = data.data;

        for ($i = 0; $i < $coun; $i++) {
            $id = $dat[$i].id;
            $user = $dat[$i].task_admin;
            $title=$dat[$i].title;
            $time=$dat[$i].time_over;
            $task_a=$dat[$i].task_a;
            $user_a=$dat[$i].user_a;
            $remark=$dat[$i].remark;
            $content=$dat[$i].task_content;
            $addtime=$dat[$i].task_time;
            $complete=$dat[$i].complete;
            $url=$dat[$i].url;
            var li_id = $id;
            $li = $("<div id="+$id+" class=\"layui-card\" id=\"card\">\n" +"<hr class=\"layui-bg-red\">"+
                "<div class=\"layui-card-header\">"+"任务详情："+"<button style=\"float: right;margin-top: 3px\" class=\"layui-btn layui-btn-primary layui-btn-sm\" onclick='del("+$id+",\""+$url+"\")'><i class=\"layui-icon\"></i></button>"+"<button id='file' class=\"layui-btn layui-btn-xs\" >"+"<a href='"+$url+"' download='file' style='color: white'>"+"附件下载"+"</a>"+"</button>"+"<a id='overTime'>"+"任务截至时间："+$time+"</a>"+"<a id='overTime'>"+"完成时间："+$complete+"</a>"+"</div>" +
                "                    <div class=\"layui-card-body\">\n" +$content+"<br>" +
                "<hr>"+"<div class=\"layui-card-header\">"+"任务："+"<a id='overTime'>"+"接受任务的成员："+$user_a+"</a>"+"</div>"+
                $task_a+
                "<hr>"+"<div class=\"layui-card-header\">"+"备注："+"</div>"+
                $remark+"<br>"+
                "                       <span id=\"time\">"+ "发布时间："+$addtime+"&nbsp;|&nbsp;"+"发布人："+$user+"</span>" +
                "                          <br>\n" +
                "                    </div>\n" +
                "                </div>");
            $("#uul").append($li);

        }
    },
    error: function (data) {
        layer.msg("暂无您发布的任务");
    }
});
function del($id,url) {
    layer.confirm('真的删除行么', function(index) {
        $.ajax({
            type:'post',
            cache:'cache',
            url:'../../php/admin/del_task.php',
            data:{
                "id":$id,
                "url":url,
            },
            dataType:'json',
            success:function (dat) {
                layer.msg(dat.message);
                var li=document.getElementById($id);
                li.remove();
            },
            error:function (dat) {
                layer.msg("删除失败")
            },

        });
    });





}

function fileDownload($url) {

    var fileD=document.getElementById("fileD");
    fileD.setAttribute("href",$url);
    fileD.setAttribute("download","file");
}
function out() {
    layer.confirm('真的要退出吗？', function(index) {
        window.location.href="../php/user/clearSession.php";
    })
}